package repository

import (
	"gorm.io/gorm"
	"shop-common/utils/pagehelper"
	"shop-common/utils/queryWrapper"
	"shop-sys/model"
	"shop-sys/pkg/vo"
)

type ISysRoleRepository interface {
	CheckUserExist(*gorm.DB, *queryWrapper.WherePair) (bool, error)
	Create(*gorm.DB, *model.SysRole) (uint, error)
	FindRole(*gorm.DB, *queryWrapper.WherePair) ([]*vo.SysRolePageVO, error)
	FindById(db *gorm.DB, roleId uint) (sysRole *model.SysRole, err error)
	FindByInRoleId(*gorm.DB, []uint) ([]uint, error)
	Updates(*gorm.DB, uint, map[string]interface{}) error
	DeleteBatch(*gorm.DB, []uint) error
	FindPage(*gorm.DB, *pagehelper.Params) ([]*vo.SysRolePageVO, error)
	Count(*gorm.DB, *pagehelper.Params) (int64, error)
}

type sysRoleRepository struct{}

func NewSysRoleRepository() ISysRoleRepository {
	return &sysRoleRepository{}
}

func (r *sysRoleRepository) Create(db *gorm.DB, role *model.SysRole) (uint, error) {
	result := db.Create(role)
	return role.RoleId, result.Error
}

func (r *sysRoleRepository) CheckUserExist(db *gorm.DB, pair *queryWrapper.WherePair) (exist bool, err error) {
	var count int64
	err = db.Select("role_id").Model(&model.SysRole{}).Where(pair.Query, pair.Args...).Count(&count).Error
	if err != nil {
		return false, err
	}
	return count > 0, nil
}

func (r *sysRoleRepository) FindPage(db *gorm.DB, params *pagehelper.Params) (list []*vo.SysRolePageVO, err error) {
	tx := queryWrapper.ReuseWherePair(db.Model(&model.SysRole{}), params.WherePair)
	return list, tx.Scopes(pagehelper.Paginate(params.Current, params.Size)).Order("created_at desc").Find(&list).Error
}

func (r *sysRoleRepository) Count(db *gorm.DB, params *pagehelper.Params) (count int64, err error) {
	tx := queryWrapper.ReuseSelect(db.Model(&model.SysRole{}), params.SelectFields)
	return count, queryWrapper.ReuseWherePair(tx, params.WherePair).Count(&count).Error
}

func (r *sysRoleRepository) FindRole(db *gorm.DB, pair *queryWrapper.WherePair) (list []*vo.SysRolePageVO, err error) {
	tx := db.Model(&model.SysRole{})
	if pair != nil {
		tx = tx.Where(pair.Query, pair.Args...)
	}
	err = tx.Order("created_at desc").Find(&list).Error
	return
}

func (r *sysRoleRepository) DeleteBatch(db *gorm.DB, roleIds []uint) error {
	return db.Delete(&model.SysRole{}, roleIds).Error
}

func (r *sysRoleRepository) Updates(db *gorm.DB, id uint, columns map[string]interface{}) error {
	return db.Model(&model.SysRole{}).Where("role_id = ?", id).Updates(columns).Error
}

func (r *sysRoleRepository) FindById(db *gorm.DB, roleId uint) (sysRole *model.SysRole, err error) {
	return sysRole, db.Model(&model.SysRole{}).First(&sysRole, roleId).Error
}

// 根据传递的角色id查询合法有数据的roleId集合
func (r *sysRoleRepository) FindByInRoleId(db *gorm.DB, isOnlineIds []uint) (roleIds []uint, err error) {
	return roleIds, db.Model(&model.SysRole{}).Select("role_id").Find(&roleIds, isOnlineIds).Error
}

//func (r *sysRoleRepository) FindByMultiple(db *gorm.DB, ids []string) (filterRoleIds []uint, err error) {
//	return filterRoleIds, db.Model(&model.SysRole{}).Select("role_id").
//		Where("FIND_IN_SET(role_id, ?)", strings.Join(ids, ",")).Find(&filterRoleIds).Error
//}
